import {ref, computed} from 'vue'
import {defineStore} from 'pinia'
import httpInstance from "@/axios/index";
import {useRouter} from 'vue-router'

export const useUserStore = defineStore('user', () => {
    const Router = useRouter()
    const userInfo = ref({})
    //用户拥有权限，用于动态路由
    const userPermission = ref([])
    const loginLog = ref()

    //动态添加路由
    const addRoutes = async () => {
        await httpInstance.get("/auth/currentSysMenuTreeList").then(responseData => {
            if (responseData.code === 200) {
                userPermission.value = responseData.data
            }
        })
        if (userPermission.value) {
            userPermission.value.forEach(item => {
                if( item.router != null){
                    console.log(item.router)
                    Router.addRoute("home",{
                        path: item.router,
                        name: item.router,
                        component: () => import(`@/views/${item.router}.vue`)
                    })
                }
            })
        }
        console.log(Router.getRoutes())
    }
    return {userInfo, userPermission, loginLog, addRoutes}
})
